//
//  HRZHomeRecommendCell.m
//  EMarket
//
//  Created by 石头 on 2018/9/6.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZHomeRecommendCell.h"
// Controllers

// Models
#import "HRZHomeItemBaseModel.h"
#import "HRZHomeRecommentBaseItem.h"
// Views
#import "HRZHomeRecommentItemCell.h"
// Vendors

// Categories

// Others


@interface HRZHomeRecommendCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *mCollection;
@property (nonatomic, strong) UILabel *mTitleLabel;

@property (nonatomic, assign) CGFloat heightED;

@end

static NSString *const HRZHomeRecommentItemCellId = @"HRZHomeRecommentItemCellID";
static NSString * const HeaderId = @"HeaderID";

@implementation HRZHomeRecommendCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self.heightED = 0;
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.mCollection];
        self.mCollection.frame = CGRectMake(0, 0, KScreenWidth, self.contentView.frame.size.height);
    }
    return self;
}

#pragma mark - ----------  collection  ----------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.model.items.count == 0) {
        return 1;
    } else {
        return self.model.items.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HRZHomeRecommentBaseItem *model = [_model.items objectAtIndex:indexPath.item];
    HRZHomeRecommentItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HRZHomeRecommentItemCellId forIndexPath:indexPath];
    cell.model = model;
    
    [self updateCollectionViewHeight:self.mCollection.collectionViewLayout.collectionViewContentSize.height];
    
    return cell;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(KScreenWidth, 40);
//}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderId forIndexPath:indexPath];
        [reusableView addSubview:self.mTitleLabel];
        return reusableView;
    } else {
        return nil;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItemAtIndexPath:withContentItem:)]) {
        [self.delegate didSelectItemAtIndexPath:indexPath withContentItem:self.model];
    }
}

#pragma mark - ----  eventRespones  ----
- (void)updateCollectionViewHeight:(CGFloat)height {
    if (self.heightED != height) {
        self.heightED = height;
        self.mCollection.frame = CGRectMake(0, 0, KScreenWidth, height);
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(updateTabelViewCellHeight:andHeight:andIndexPath:)]) {
            [self.delegate updateTabelViewCellHeight:self andHeight:height andIndexPath:self.indexPath];
        }
    }
    
}

- (NSString *)titleWithModel{
    NSString *title;
    switch (_model.type) {
        case HomeItemTypeCarousel: {
            title = @"";
            break;
        }
        case HomeItemTypeTheme:
            title = @"主题";
            break;
            
        case HomeItemTypeHotClassify: {
            title = @"热门分类";
            break;
        }
        case HomeItemTypeBigbrand: {
            title = @"大牌";
            break;
        }
    }
    return title;
}

#pragma mark - ----  Getter/Setter  ----
- (void)setModel:(HRZHomeItemModel *)model {
    
    _model = model;
    
}

- (UICollectionView *)mCollection{
    if (!_mCollection) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        CGFloat width = (KScreenWidth - 50)/ 4;
        layout.itemSize = CGSizeMake(width, width * 1.4);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
        _mCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _mCollection.delegate = self;
        _mCollection.dataSource = self;
        
        [_mCollection registerClass:[HRZHomeRecommentItemCell class] forCellWithReuseIdentifier:HRZHomeRecommentItemCellId];
        [_mCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderId];
        
        _mCollection.backgroundColor = [UIColor whiteColor];
    }
    return _mCollection;
}

- (UILabel *)mTitleLabel{
    if (!_mTitleLabel) {
        _mTitleLabel = [[UILabel alloc] init];
        _mTitleLabel.font = PFR11Font;
        _mTitleLabel.text = [self titleWithModel];
    }
    return _mTitleLabel;
}


@end
